package com.zhulin.android.edoctor.fragment;

import com.zhulin.android.edoctor.MainActivity;
import com.zhulin.android.edoctor.R;
import com.zhulin.android.edoctor.jsonobject.INGetCaseInfo;
import com.zhulin.android.edoctor.jsonobject.JOCaseInfoModel;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseInfo;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList.GetCaseListModel;
import com.zhulin.android.edoctor.net.AsyncHttpPost;
import com.zhulin.android.edoctor.net.DefaultThreadPool;
import com.zhulin.android.edoctor.parsehandler.GetCaseInfoParseHandler;
import com.zhulin.android.edoctor.util.Command;
import com.zhulin.android.edoctor.util.IntentContainer;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class FragmentCaseListSubitem extends BaseFragment implements OnClickListener{
	
	private MainActivity mMainActivity=null;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mMainActivity=(MainActivity) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.item_caselist_subitem_detail, container, false);
		
		Bundle bundle=getArguments();
		GetCaseListModel joCaseInfoModel=(GetCaseListModel)bundle.getSerializable(IntentContainer.INTENT_DATA_KEY);
		
		initView(view);
		
		String caseID= joCaseInfoModel.getCaseID();
		requestDetail(caseID);
		return view;
	}
	
	private void requestDetail(String caseID){
		ShowProgressDialog("", getResources().getString(R.string.requesting));
		INGetCaseInfo inGetCaseInfo=new INGetCaseInfo();
		inGetCaseInfo.setCaseID(caseID);
		AsyncHttpPost asyncHttpPost=new AsyncHttpPost(new GetCaseInfoParseHandler(), Command.SERVER_ADDRESS+Command.ACTION_GetCaseInfo
				, inGetCaseInfo.getClassInfo(), requestResultCallback);
		DefaultThreadPool.getInstance().execute(asyncHttpPost);
		listRequest.add(asyncHttpPost);
	}
	
	@Override
	public void onSuccess(Object o) {
		super.onSuccess(o);
		OUTGetCaseInfo outGetCaseInfo=(OUTGetCaseInfo) o;
		if(outGetCaseInfo.getResult()==1){
			loadData(outGetCaseInfo);
		}else{
			Toast(getActivity(), ""+outGetCaseInfo.getDescription(), Toast.LENGTH_LONG);
		}
	}
	
	private EditText edtName,edtSex,edtAge,edtPlace,edtSelftDescription,edtPictureDescription,edtFunction,edtResult;
	private EditText edtComment;
	private void initView(View view){
		edtName=(EditText)view.findViewById(R.id.edtName);
		edtSex=(EditText)view.findViewById(R.id.edtSex);
		edtAge=(EditText)view.findViewById(R.id.edtAge);
		
		edtPlace=(EditText)view.findViewById(R.id.edtPlace);
		edtSelftDescription=(EditText)view.findViewById(R.id.edtSelftDesription);
		edtPictureDescription=(EditText)view.findViewById(R.id.edtPictureDescription);
		edtFunction=(EditText)view.findViewById(R.id.edtFunction);
		edtResult=(EditText)view.findViewById(R.id.edtResult);
		
		edtComment=(EditText)view.findViewById(R.id.edtComment);
		
		view.findViewById(R.id.btnZhan).setOnClickListener(this);
		view.findViewById(R.id.btnComment).setOnClickListener(this);
		view.findViewById(R.id.btnCommitComment).setOnClickListener(this);
	}
	
	private void loadData(OUTGetCaseInfo joCaseInfoModel){
		edtName.setText(""+joCaseInfoModel.getDiagnose());
		edtSex.setText(""+joCaseInfoModel.getSex());
		edtAge.setText(""+joCaseInfoModel.getAge());
		
		edtPlace.setText(""+joCaseInfoModel.getTreatPlan());
		edtSelftDescription.setText(""+joCaseInfoModel.getComplain());
		edtPictureDescription.setText(""+joCaseInfoModel.getAbstract());
		edtFunction.setText(""+joCaseInfoModel.getTreatPlan());
		edtResult.setText(""+joCaseInfoModel.getTreatResult());
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btnComment:{
			mMainActivity.addFragmentOfUserLoginSelectList();
			break;
		}case R.id.btnCommitComment:{
			mMainActivity.addFragmentOfUserLoginSelectList();
			break;
		}case R.id.btnZhan:{
			mMainActivity.addFragmentOfUserLoginSelectList();
			break;
		}
		}
	}
}
